package cn.bdqn.service.impl;

import cn.bdqn.mapper.CstServiceMapper;
import cn.bdqn.pojo.CstService;
import cn.bdqn.pojo.Customer;
import cn.bdqn.service.CstServiceService;
import cn.bdqn.service.CustomerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class CstServiceServiceImpl implements CstServiceService {
    @Resource
    CstServiceMapper cstServiceMapper;
    @Resource
    CustomerService customerService;
    @Override
    public void addCstService(CstService cstService) {
        //创建时间
        cstService.setSvrCreateDate(new Date());
        //状态
        cstService.setSvrStatus("新创建");

        //设置客户的姓名字段
        Customer customer = customerService.getCustomer(cstService.getSvrCustNo());
        cstService.setSvrCustName(customer.getCustName());

        cstServiceMapper.insert(cstService);

    }

    @Override
    public IPage<CstService> getCstServicesByPage(String custName, String title, String type, String status, Integer pageNo, Integer pageSize) {
        Page<CstService> page=new Page<>(pageNo,pageSize);

        //查询条件
        QueryWrapper<CstService> qw=new QueryWrapper<>();
        qw.eq("svr_status",status);
        qw.like(custName!=null&&!"".equals(custName),"svr_cust_name",custName);
        qw.like(title!=null&&!"".equals(title),"svr_title",title);
        qw.eq(type!=null&&!"".equals(type),"svr_type",type);

        IPage<CstService> cstServiceIPage = cstServiceMapper.selectPage(page, qw);
        return cstServiceIPage;
    }

    @Override
    public void updateCstService(CstService cstService) {
        cstServiceMapper.updateById(cstService);
    }

    @Override
    public void deleteCstService(Long id) {
        cstServiceMapper.deleteById(id);
    }

    @Override
    public CstService getCstService(Long id) {
        return  cstServiceMapper.selectById(id);
    }


}
